package com.jsmy.softapp.adapter;

import android.app.Activity;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.jsmy.softapp.R;
import com.jsmy.softapp.bean.TXLBean;
import com.jsmy.softapp.customview.RoundImageView;
import com.jsmy.softapp.net.NetWork;

import java.util.List;

/**
 * Created with Android Studio.
 * User: wei_cd.
 * Date: 2018/4/18.
 * Time: 16:29.
 * To change for file | settings | file and code templates.
 */

public class DataFragmentAdapter extends BaseExpandableListAdapter {

    private Activity context;
    private List<TXLBean.DataBean.ListBeanX> list;

    public DataFragmentAdapter(Activity context, List<TXLBean.DataBean.ListBeanX> list) {
        this.context = context;
        this.list = list;
        this.mIndicators = new SparseArray<>();
    }

    @Override
    public int getGroupCount() {
        return list.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return list.get(groupPosition).getList().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return list.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return list.get(groupPosition).getList().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        GroupViewHolder group;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_data_item_group, parent, false);
            group = new GroupViewHolder();
            group.tvTitle = convertView.findViewById(R.id.tv_title);
            group.imgIndicator = convertView.findViewById(R.id.img_indicator);
            convertView.setTag(group);
        } else {
            group = (GroupViewHolder) convertView.getTag();
        }
        mIndicators.put(groupPosition, group.imgIndicator);
        setIndicatorState(groupPosition, isExpanded);
        group.tvTitle.setText(list.get(groupPosition).getTitleid());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder child;
        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.fragment_data_item_child, parent, false);
            child = new ChildViewHolder();
            child.img = convertView.findViewById(R.id.img);
            child.tvTitle = convertView.findViewById(R.id.tv_title);
            child.tvPhone = convertView.findViewById(R.id.tv_phone);
            child.tvContent = convertView.findViewById(R.id.tv_content);
            child.tvPlant = convertView.findViewById(R.id.tv_plant);
            convertView.setTag(child);
        } else {
            child = (ChildViewHolder) convertView.getTag();
        }
        child.tvTitle.setText(list.get(groupPosition).getList().get(childPosition).getXm());
        child.tvPhone.setText(list.get(groupPosition).getList().get(childPosition).getDh());
        child.tvPlant.setText(list.get(groupPosition).getList().get(childPosition).getPlan());
        child.tvContent.setText(list.get(groupPosition).getList().get(childPosition).getNote());
        NetWork.setImageGlide(context,list.get(groupPosition).getList().get(childPosition).getIco(),child.img);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    static class GroupViewHolder {
        TextView tvTitle;
        ImageView imgIndicator;
    }

    static class ChildViewHolder {
        RoundImageView img;
        TextView tvTitle;
        TextView tvPhone;
        TextView tvContent;
        TextView tvPlant;
    }

    //                用于存放Indicator的集合
    private SparseArray<ImageView> mIndicators;

    //            根据分组的展开闭合状态设置指示器
    public void setIndicatorState(int groupPosition, boolean isExpanded) {
        if (isExpanded) {
            mIndicators.get(groupPosition).setImageResource(R.drawable.ic_menu_chevronright);
        } else {
            mIndicators.get(groupPosition).setImageResource(R.drawable.ic_menu_chevrondown);
        }
    }
}
